package com.wiscamp.ninechapters.problems.domain.models.exams;

import com.wiscamp.ninechapters.ddd.AggregateRoot;
import lombok.Data;
import lombok.EqualsAndHashCode;

import java.io.Serial;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Collection;
import java.util.stream.Collectors;

/**
 * 考试
 */
@Data
@EqualsAndHashCode
public class Exam implements AggregateRoot {
    @Serial
    private static final long serialVersionUID = -7509239069614462786L;

    /**
     * 考试编号
     */
    private long examId = 0;

    /**
     * 机构编号
     */
    private long organizationId = 0;

    /**
     * 学科编号
     */
    private long subjectId = 0;

    /**
     * 语言代码
     */
    private String languageCode = "";

    /**
     * 考试代码
     */
    private String examCode = "";

    /**
     * 考试名称
     */
    private String examName = "";

    /**
     * 总分
     */
    private double totalScore = 0;

    /**
     * 总时间限制
     */
    private int totalTimeLimit = 0;

    /**
     * 总题目数
     */
    private int totalProblemQuantity = 0;

    /**
     * 备注
     */
    private String remark = "";

    /**
     * 是否可用
     */
    private boolean enabled = true;

    /**
     * 是否删除
     */
    private boolean deleted = false;

    /**
     * 创建用户
     */
    private long creatorId = 0;

    /**
     * 创建时间
     */
    private LocalDateTime creationTime;

    /**
     * 编辑用户
     */
    private long modifierId = 0;

    /**
     * 编辑时间
     */
    private LocalDateTime modificationTime;

    private String organizationName = "";

    private String subjectName = "";

    private String creatorName = "";

    private String modifierName = "";

    private Collection<ExamTypeRelation> examTypeRelations = new ArrayList<>();

    private Collection<ScoringRule> scoringRules = new ArrayList<>();

    public void enable() {
        if (!this.enabled) {
            this.enabled = true;
        }
    }

    public void disable() {
        if (this.enabled) {
            this.enabled = false;
        }
    }

    public void delete() {
        if (!this.deleted) {
            this.deleted = true;
        }
    }

    public void createBy(long operatorId) {
        this.creatorId = operatorId;
        this.creationTime = LocalDateTime.now();
        this.modifierId = operatorId;
        this.modificationTime = LocalDateTime.now();
    }

    public void updateBy(long operatorId) {
        this.modifierId = operatorId;
        this.modificationTime = LocalDateTime.now();
    }

    public Collection<Long> getProblemTypeIds() {
        return this.examTypeRelations.stream().map(ExamTypeRelation::getProblemTypeId).collect(Collectors.toList());
    }
}